﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WowGuildSite.Armory
{
    public enum     ItemSlot
    {
        None        = -2,
        Inventory   = -1,
        Helmet      = 0,
        Necklace    = 1,
        Shoulder    = 2,
        Shirt       = 3,
        Chest       = 4,
        Belt        = 5,
        Pants       = 6,
        Boots       = 7,
        Bracers     = 8, // Armschienen
        Gauntlets   = 9, // Handschuhe
        Ring1       = 10,
        Ring2       = 11,
        Trinket1    = 12,
        Trinket2    = 13,
        Cape        = 14,
        MainHand    = 15,
        OffHand     = 16,
        Ranged      = 17,
        Tabard      = 18, // Wappenrock    
    }

    public enum     ItemQuality
    {
        Poor        = 0,
        Common      = 1,
        Uncommon    = 2,
        Rare        = 3,
        Epic        = 4,
        Legendary   = 5,
        Artifact    = 6,
    }

    public enum     ItemBindType
    {
        None         = 0,
        BindOnPickup = 1,
        BindOnEquip  = 2,
        Quest        = 4,
    }

    public class ArmoryItem : ArmoryBase
    {
        private ArmoryItemId        id;
        private ArmoryXmlDocument   itemInfoDocument;
        private ArmoryXmlDocument   itemTooltipDocument;

        public ArmoryItemId         Id
        {
            get { return id; }
        }

        public ArmoryIcon           Icon
        {
            get
            {
                return Accessor.GetIcon(IconType.Item, id);
            }
        }

        public ItemBindType         Bonding
        {
            get
            {
                return (ItemBindType) itemTooltipDocument.GetElementInt("/page/itemTooltips/itemTooltip/bonding");
            }
        }

        public string               LocalizedName
        {
            get
            {
                return itemTooltipDocument.GetElementText("/page/itemTooltips/itemTooltip/name");
            }
        }

        public string               LocalizedType
        {
            get
            {
                return itemTooltipDocument.GetElementText("/page/itemTooltips/itemTooltip/equipData/subclassName");
            }
        }

        public int                  RequiredLevel
        {
            get
            {
                return itemTooltipDocument.GetElementInt("/page/itemTooltips/itemTooltip/requiredLevel");
            }
        }

        public ItemSlot             Slot
        {
            get
            {
                return
                    (ItemSlot)
                    itemTooltipDocument.GetElementInt("/page/itemTooltips/itemTooltip/equipData/inventoryType");
            }
        }

        public int                  ItemLevel
        {
            get
            {
                return itemInfoDocument.GetAttributeInt("/page/itemInfo/item/@level");
            }
        }

        public ItemQuality          Quality
        {
            get
            {
                return (ItemQuality)itemTooltipDocument.GetElementInt("/page/itemTooltips/itemTooltip/overallQualityId");
            }
        }

        public int                  Armor
        {
            get
            {
                return itemTooltipDocument.GetElementInt("/page/itemTooltips/itemTooltip/armor");
            }
        }

        public int                  Durability
        {
            get
            {
                return itemTooltipDocument.GetAttributeInt("/page/itemTooltips/itemTooltip/durability/@max");
            }
        }

        public ArmoryItemDamage     Damage
        {
            get
            {
                return
                    ArmoryItemDamage.FromElement(
                        itemTooltipDocument.GetElement("/page/itemTooltips/itemTooltip/damageData"));
            }
        }

        public ArmoryItemSockets    Sockets
        {
            get
            {
                return
                    ArmoryItemSockets.FromElement(
                        itemTooltipDocument.GetElement("/page/itemTooltips/itemTooltip/socketData"));
            }
        }

        internal ArmoryItem(   ArmoryAccessor accessor, 
                               ArmoryItemId id )
            : base(accessor)
        {
            this.id                     = id;
            this.itemInfoDocument       = new ArmoryXmlDocument(delegate() { return Accessor.DataSource.GetItemInfoData(id.MainId); });
            this.itemTooltipDocument    = new ArmoryXmlDocument(delegate() { return Accessor.DataSource.GetItemTooltipData(id.MainId); });
        }
    }
}
